const host = 'http://book.web-jshtml.cn/view'

/**
 * 封装微信的request
 * form: 'application/x-www-form-urlencoded'
 */
const request = function(params, url, data = {}, method = "GET", contentType = 'json') {
  return new Promise(function(resolve, reject) {
    wx.request({
      url: host + params.url,
      data: params.data,
      method: params.method || 'post',
      header: {
        // 'Content-Type': contentType.toLowerCase() == 'json' ? "application/json" : "application/x-www-form-urlencoded"
        'Content-Type': "application/json",
		'Authorization': wx.getStorageSync('token') || ''
      },
      success: function(res) {
		  const data = res.data
        if (data.error_code == 0) {
          resolve(data);
        } else {
          reject(res.data);
        }
      },
      fail: function(err) {
        reject(err)
      }
    })
  });
}

export default request;